<?php

namespace App\Modules\Manage\Model;

//use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
//use Cache;

class PigOrderModel extends Model
{
    protected $table = 'pig_order';
    protected $fillable = ['id','user_id','product_id','num','address','receiver','pay_status','logistics','express_code','is_show','create_time'];
    public  $timestamps = false;


    static function cartAdd(array $order)
    {
        $result = self::create($order);
        return $result;
    }
    //判断是否已在购物车中
    static function existed(array $data)
    {
        $result = self::where(['user_id'=>$data['user_id'],'product_id'=>$data['product_id'],'pay_status'=>0])->first();
        if ($result) {
            return 1;
        } else {
            return 0;
        }
    }

    //获取购物车列表
    static function cartAll($user_id)
    {
        $results = Self::from('pig_order as po')
            ->leftJoin('pig_product as pp','po.product_id','=','pp.id')
            ->leftJoin('pig_file as pf','pp.id','=','pf.product_id')
            ->groupBy('po.product_id')
            ->where(['po.user_id'=>$user_id,'po.pay_status'=>0,'pp.is_show'=>1])
            ->select('po.id','pf.file_path','pp.title','pp.cost','po.num')
            ->get();
        return $results;
    }

    static function cartDel(array $ids)
    {
        $result = self::whereIn('id',$ids)
            ->delete();
        return $result;
    }
}
